
//登录
function reset() {
	$("#frm_login")[0].reset();
}

function login() {
	var uname = $("#uname").val();
	if(uname.length == 0) {
		layer.tips("用户名不能为空!", "#uname", {
			tips: [2, "#FF0000"],
			time: 1000
		});
		return false;
	}

	var upwd = $("#upwd").val();
	//正则表达式验证密码
	var patrn = /^(\w){6,20}$/
	if(!patrn.exec(upwd)) {
		layer.tips("密码输入有误!", "#upwd", {
			tips: [2, "#FF0000"],
			time: 1000
		});
		return false;
	}

	//验证验证码
	var vcode = $('#vcode').val();
	console.log(uname, upwd, vcode);
	var reg = /^[A-Za-z0-9]{4}$/
	if(!reg.exec(vcode)) {
		layer.tips("验证码长度有误或包含非法字符!", "#vcode", {
			tips: [2, "#FF0000"],
			time: 1000
		});
		return false;
	}
	console.log(vcode, code);
	if(vcode.toLowerCase() != code.toLowerCase()) {
		layer.tips("验证码输入错误!", "#vcode", {
			tips: [1, "#ff0000"],
			time: 1000
		});
		return false;
	}

	$.ajax({
		url: "dataJson/login.json",
		type: "get",
		dataType: "json",
		success: function(result) {
			var userinfo = result.data;
			var temp = false;
			$.each(userinfo, function(index,value) {
				console.log(value.uname);
				if(uname == value.uname && upwd == value.upwd){
					temp = true;
				}
			});
			if(temp == false) {
				layer.msg("登录失败!", {
					icon: 2,
					time: 2000
				});
			} else {
					window.location.href="manager.html";
				var state = $("#rememberPwd").prop("checked");
				if(state == true) {
					localStorage.setItem("uname", uname);
					localStorage.setItem("upwd", upwd);				
				} else {
					localStorage.clear();
			}
				sessionStorage.setItem("uname", uname);
			
			}
	
	},
		error:function(){
			layer.msg("请求服务器失败!", {
				icon: 2,
				time: 2000
			});
		}
	});
	return true;
}



//注册
function reset_register() {
	$("#frm_register")[0].reset();
}

function login_register() {
	var uname = $('#r_uname').val();
	var upwd = $('#r_upwd').val();
	var rupwd = $('#r_cupwd').val();
	var remail = $('#r_email').val();
	var patrn_name = /^([a-zA-Z0-9]|[._]){6,20}$/;
	var patrn_upwd = /^([a-zA-Z0-9]|[._]){6,12}$/;
	if(!patrn_name.exec(uname)) {
		layer.tips("用户名输入有误！只能输入6-20个数字字母或者._,请重新输入！", "#r_uname", {
			tips: 2,
			time: 1000
		})
		return;
	}

	if(!patrn_upwd.exec(upwd)) {
		layer.tips("密码输入有误！只能输入6-12个数字字母或者._,请重新输入！", "#r_upwd", {
			tips: 2,
			time: 1000
		})
		return;
	}

	if(upwd != rupwd) {
		layer.tips("密码与确认密码不一致!", "#r_cupwd", {
			tips: 2,
			time: 1000
		})
		return;
	}
	var patrn_e = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
	if(!patrn_e.exec(remail)) {
		layer.tips("邮箱格式不正确!", "#r_email", {
			tips: 2,
			time: 1000
		})
		return;
	}
	//	注册访问后台
	$.ajax({
		type: "get",
		url: "dataJson/loginregister.json",
		data: {},
		dataType: "json",
		success: function(result) {
			var temp = result.code;
			var data = result.data.msg;
			if(temp == 1) {
				layer.msg(data, {
					icon: 1,
					time: 1000
				});
			    window.location.href="login new.html";
			} else {
				layer.msg(" 注册失败！", {
					icon: 2
				});
			}
		},
		error: function() {
			layer.msg("访问服务器失败！", {
				icon: 2
			});
		}
	});
}